package com.wanlian.dao.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.RoleGroupUserDao;
import com.wanlian.entity.Authority;
import com.wanlian.entity.RoleGroupUser;
import com.wanlian.model.ResultModel;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class RoleGroupUserDaoImpl extends GenericDaoImpl<RoleGroupUser> implements RoleGroupUserDao {


    public RoleGroupUserDaoImpl() {
        super(RoleGroupUser.class);
    }


    @Override
    public RoleGroupUser queryByGid(int groupId, int userId) {
        String hql = String.format("from %s where groupid=? and userid=? ", entityClassName());
        RoleGroupUser roleGroupUser = this.queryForObject(hql, new Object[]{groupId,userId});
        return roleGroupUser;
    }

    @Override
    public RoleGroupUser queryByUid(int userId) {
        String hql = String.format("from %s where   userid=? ", entityClassName());
        RoleGroupUser roleGroupUser = this.queryForObject(hql, new Object[]{userId});
        return roleGroupUser;
    }

    @Override
    public ResultModel queryByParam(PageCondition pageCondition) {

        StringBuffer sqlWhere = new StringBuffer(" where 1=1 ");
        ArrayList<Object> params = new ArrayList<Object>();
        if (pageCondition.isExistByKey("authorityName")) {
            if (pageCondition.getConditions().get("authorityName") != null && pageCondition.getConditions().get("authorityName") != "") {
                sqlWhere.append(" and authorityname like ?");
                params.add("%" + pageCondition.getConditions().get("authorityName") + "%");
            }
        }

        List<RoleGroupUser> models = this.queryForListByPage("from " + entityClassName() + sqlWhere + " order by lastupdatetime desc, createtime desc", params.toArray(), pageCondition.getPage());
        String hql = String.format("select count(*) from %s " + sqlWhere, entityClassName());
        Long totalSize = this.queryCount(hql, params.toArray());
        ResultModel resultModel = new ResultModel();
        resultModel.setCount(totalSize);
        resultModel.setObject(models);
        return resultModel;
    }
}
